package Threads.ReentrantReadWriteLock.JUC_other;

import Threads.Utils.log;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//还有一个cyclicBarrier


//这个countdownlatch的原理相对简单
//最开始的构造参数中传入的值，就是设置的sync同步队列的state的值
//调用await的方法，进入同步队列等待
//调用countdown的方法，会让state减1，当state变成0之后，会唤醒同步队列中的线程

public class Countdownlatch_test {
    public static void main(String[] args) throws InterruptedException {
        CountDownLatch latch=new CountDownLatch(3);
        ExecutorService executorService = Executors.newFixedThreadPool(3);
        executorService.submit(()->{
            try {
                System.out.println("t1 running---");
                Thread.sleep(1000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            try {
                System.out.println("t2 running---");
                Thread.sleep(2000);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        executorService.submit(()->{
            try {
                System.out.println("t3 running---");
                Thread.sleep(1500);
                latch.countDown();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        //会等待上面的三个线程都执行完，才会继续执行
        latch.await();
        System.out.println("main  running-------");
    }
}
